<?php
/**
 * @author: Đặng Văn Trường
 * @since: 22/07/2014
 * @todo : Model Van ban den
 */
class Module_Models_ReceiveStaff extends Zend_Db_Table {	
	
	protected $_name="receivehandlestaff";
    protected $_primary_name;
    protected $_primary = "ReceiveHandleStaffID";
    protected $_detail = 'receivedetail';
    public function __construct()
    {
        parent::__construct();
        $this->_primary_name = $this->_primary;
    }
	public function getAll($params) {

        $pagenum = $params['pagenum'];
        $pagesize = $params['pagesize'];
        $start = $pagenum * $pagesize;

        // Lay tong row
        $query = $this->select()->from($this, '')->columns(array('total' => new Zend_Db_Expr('COUNT(*)')));
        $getTotal = $query;
        $query = $this->select()->from($this)->order('Number DESC')->limit($pagesize, $start);

        // Filter
        if (isset($params['filterscount']))
        {
            $filterscount = $params['filterscount'];
            
            if ($filterscount > 0)
            {
                $where = " WHERE (";
                $tmpdatafield = "";
                $tmpfilteroperator = "";
                for ($i=0; $i < $filterscount; $i++)
                {
                    // get the filter's value.
                    $filtervalue = $params["filtervalue" . $i];
                    // get the filter's condition.
                    $filtercondition = $params["filtercondition" . $i];
                    // get the filter's column.
                    $filterdatafield = $params["filterdatafield" . $i];
                    // get the filter's operator.
                    $filteroperator = $params["filteroperator" . $i];
                    
                    if ($tmpdatafield == "")
                    {
                        $tmpdatafield = $filterdatafield;           
                    }
                    else if ($tmpdatafield <> $filterdatafield)
                    {
                        $where .= ")AND(";
                    }
                    else if ($tmpdatafield == $filterdatafield)
                    {
                        if ($tmpfilteroperator == 0)
                        {
                            $where .= " AND ";
                        }
                        else $where .= " OR ";  
                    }
                    
                    // build the "WHERE" clause depending on the filter's condition, value and datafield.
                    switch($filtercondition)
                    {
                        case "NOT_EMPTY":
                        case "NOT_NULL":
                            $where .= " " . $filterdatafield . " NOT LIKE '" . "" ."'";
                            break;
                        case "EMPTY":
                        case "NULL":
                            $where .= " " . $filterdatafield . " LIKE '" . "" ."'";
                            break;
                        case "CONTAINS_CASE_SENSITIVE":
                            $where .= " BINARY  " . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
                            break;
                        case "CONTAINS":
                            $where .= " " . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
                            break;
                        case "DOES_NOT_CONTAIN_CASE_SENSITIVE":
                            $where .= " BINARY " . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
                            break;
                        case "DOES_NOT_CONTAIN":
                            $where .= " " . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
                            break;
                        case "EQUAL_CASE_SENSITIVE":
                            $where .= " BINARY " . $filterdatafield . " = '" . $filtervalue ."'";
                            break;
                        case "EQUAL":
                            $where .= " " . $filterdatafield . " = '" . $filtervalue ."'";
                            break;
                        case "NOT_EQUAL_CASE_SENSITIVE":
                            $where .= " BINARY " . $filterdatafield . " <> '" . $filtervalue ."'";
                            break;
                        case "NOT_EQUAL":
                            $where .= " " . $filterdatafield . " <> '" . $filtervalue ."'";
                            break;
                        case "GREATER_THAN":
                            $where .= " " . $filterdatafield . " > '" . $filtervalue ."'";
                            break;
                        case "LESS_THAN":
                            $where .= " " . $filterdatafield . " < '" . $filtervalue ."'";
                            break;
                        case "GREATER_THAN_OR_EQUAL":
                            $where .= " " . $filterdatafield . " >= '" . $filtervalue ."'";
                            break;
                        case "LESS_THAN_OR_EQUAL":
                            $where .= " " . $filterdatafield . " <= '" . $filtervalue ."'";
                            break;
                        case "STARTS_WITH_CASE_SENSITIVE":
                            $where .= " BINARY " . $filterdatafield . " LIKE '" . $filtervalue ."%'";
                            break;
                        case "STARTS_WITH":
                            $where .= " " . $filterdatafield . " LIKE '" . $filtervalue ."%'";
                            break;
                        case "ENDS_WITH_CASE_SENSITIVE":
                            $where .= " BINARY " . $filterdatafield . " LIKE '%" . $filtervalue ."'";
                            break;
                        case "ENDS_WITH":
                            $where .= " " . $filterdatafield . " LIKE '%" . $filtervalue ."'";
                            break;
                    }
                                    
                    if ($i == $filterscount - 1)
                    {
                        $where .= ")";
                    }
                    
                    $tmpfilteroperator = $filteroperator;
                    $tmpdatafield = $filterdatafield;           
                }
                // build the query.
                $query = "SELECT * FROM $this->_name ".$where;
                $filterquery = $query;
                $getTotal = "SELECT COUNT(*) as total FROM $this->_name ".$where;
                $query = "SELECT * FROM $this->_name ".$where." LIMIT $start, $pagesize";
            }
        }
        // Sort
        if (isset($params['sortdatafield'])) {
            $sortfield = $params['sortdatafield'];
            $sortorder = $params['sortorder'];
            if ($sortorder != '') {
                if ($params['filterscount'] == 0) {
                    if ($sortorder == "desc") {
                        $query = "SELECT * FROM $this->_name ORDER BY" . " " . $sortfield . " DESC LIMIT $start, $pagesize";
                    } else if ($sortorder == "asc") {
                        $query = "SELECT * FROM $this->_name ORDER BY" . " " . $sortfield . " ASC LIMIT $start, $pagesize";
                    }
                } else {
                    if ($sortorder == "desc") {
                        $filterquery .= " ORDER BY" . " " . $sortfield . " DESC LIMIT $start, $pagesize";
                    } else if ($sortorder == "asc") {
                        $filterquery .= " ORDER BY" . " " . $sortfield . " ASC LIMIT $start, $pagesize";
                    }
                    $query = $filterquery;
                }
            }
        }

        // Lay tong
        $rows = $this->getAdapter()->fetchRow($getTotal);
        $total_rows = $rows['total'];
        // Lay du lieu
        $rows = $this->getAdapter()->fetchAll($query);
        $data[] = array(
            'TotalRows' => $total_rows,
            'Rows' => $rows
        );

        return $data;
    }
	
    public function selectRow($id){
        if($id){
            $query = $this->select()->from($this)->where($this->_primary_name.' = ?', $id);
            $rows = $this->getAdapter()->fetchRow($query);
            $rows['ReceivedDate'] = date('d/m/Y',strtotime($rows['ReceivedDate']));
            $rows['SendDate'] = date('d/m/Y',strtotime($rows['SendDate']));
        }else{
            $rows = array($this->_primary_name =>''
                            // ,'Code' =>''
                            // ,'Name' =>''
                            // ,'Orders' =>$orders['Orders']+1
                            // ,'Status' =>'1'
                            // ,'ListtypeID' =>''
                        );
        }
        return $rows;
    }

    public function deleteRows($listId){
        $aListID = explode(',', $listId);
        // Xoa file dinh kem
        // $modeFile = new System_Models_File();
        $where = $this->getAdapter()->quoteInto("$this->_primary_name IN (?)", $aListID);
        // echo $where; die;
        return $this->delete($where);
    }
}
?>
